package net.sevear.chaos.download
{
	import com.adobe.serialization.json.JSON;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import net.sevear.chaos.vo.TrackInfo;
	
	public class XiamiProxy extends EventDispatcher
	{
		public function XiamiProxy(target:IEventDispatcher=null)
		{
			super(target);
		}
		public static const STRTRICT_MODE:int = 0;
		public static const LOOSE_MODE:int = 1;
		
		private static const SEARCH_URL:String = "http://www.xiami.com/app/iphone/search?key=";
		private static const SONG_INF_URL:String = "http://www.xiami.com/app/iphone/song?id=";
		
		private var searchMode:int = STRTRICT_MODE;
		private var curSearchTrack:TrackInfo;
		private var loader:URLLoader;
		public function searchSong(songName:String,artistName:String = "",albumName:String=""){
			curSearchTrack = new TrackInfo();
			curSearchTrack.name = songName;
			curSearchTrack.artistName = artistName;
			curSearchTrack.albumName = albumName;
			var trackName:String  = curSearchTrack.name;
			if(searchMode == LOOSE_MODE){
				trackName= trackName.replace(/\([a-zA-Z 0-9]*\)/g,"");
				trace(trackName);
			}
			var url:String = SEARCH_URL+encodeURI(trackName+" "+curSearchTrack.artistName);
			request = new URLRequest(url);
			request.method = URLRequestMethod.GET;
			if(loader == null){
				loader = new URLLoader();
				loader.addEventListener(Event.COMPLETE, completeHandler);
				loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler)
			}
			loader.load(request);
		}
		
		public function getSearchSongRst():TrackInfo{
			return curSearchTrack;
		}
		
		public function searchAlbum(albumName:String,artistName:String = ""){
			
		}
		
		private function completeHandler(event:Event):void {
			var rst:Object = JSON.decode((event.target as URLLoader).data as String);
			if(rst.status != null && rst.status=="ok"){
				var songs:Array = rst.songs as Array;
				trace("search result num:"+songs.length);
				_rstTrackObj = selectFitSong(songs,curSearchTrack);
				if(_rstTrackObj==null){
					dispatchEvent(new XiamiSearchCompleteEvent(XiamiSearchCompleteEvent.SEARCH_SONG_FAILED));
				}
				else{
					dispatchEvent(new XiamiSearchCompleteEvent(XiamiSearchCompleteEvent.SEARCH_SONG_SUCCESS));
				}
			}
		}
		
		
		//FIXME:
		private function selectFitSong(rst:Array,targetTrack:TrackItemInfo):Object{
			var rstInfo:TrackItemRstInfo;
			var highestPoint:int=0;
			var highestPointSong:Object ;
			for each(var song:Object in rst){
				var curPoint:int=0;
				var name:String = song.name as String;
				if(name == targetTrack.name) 
					curPoint+=100;
				else {
					if(searchMode == LOOSE_MODE){
						if(targetTrack.name.search(name)>=0) curPoint += 50;
					}
				}
				var artist:String = song.artist_name as String;
				if(artist.search(targetTrack.artistName)>=0) 
					curPoint +=100;
				if(highestPoint<curPoint){
					highestPoint = curPoint;
					highestPointSong = song;
				}
			}
			
			return highestPointSong;
		}
		
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			trace("ioErrorHandler: " + event);
		}
	}
}